package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.entity.BusGoods;
import com.example.warehouse.bus.pojo.entity.BusProvider;
import com.example.warehouse.bus.pojo.param.BusCustomerAddNewParam;
import com.example.warehouse.bus.pojo.param.BusGoodsAddNewParam;
import com.example.warehouse.bus.pojo.vo.BusGoodsStandardVO;
import com.example.warehouse.bus.pojo.vo.BusProviderStandardVO;
import com.example.warehouse.bus.service.IBusGoodsService;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Linger
 * @date 2023-07-07 9:03
 * @desciption:基础板块商品管理控制层
 */
@Slf4j
@RequestMapping("/busGoods")
@RestController
@Api(tags = "2.3 基础管理-商品管理")
public class BusGoodsController {

    @Autowired
    private IBusGoodsService busGoodsService;

    public BusGoodsController() {
        log.info("创建控制器对象:BusGoodsController");
    }

    @ApiOperation("添加商品")
    @ApiOperationSupport(order = 102)
    @PreAuthorize("hasAuthority('/busGoods/add-new')")
    @PostMapping("/add-new")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "providerId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "promitCode", value = "批准文号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "producePlace", value = "商品产地", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "规格", dataType = "String"),
            @ApiImplicitParam(name = "goodsPackage", value = "包装", dataType = "String"),
            @ApiImplicitParam(name = "productCode", value = "生产批号", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "商品描述", dataType = "String"),
            @ApiImplicitParam(name = "price", value = "销售价格", dataType = "String"),
            @ApiImplicitParam(name = "number", value = "数量", dataType = "String"),
            @ApiImplicitParam(name = "dangerNum", value = "库存预警值", dataType = "String"),
            @ApiImplicitParam(name = "goodsImg", value = "商品图片", dataType = "String"),
            @ApiImplicitParam(name = "enable", value = "是否启用", dataType = "Integer")
    })
    public JsonResult addNewBusGoods(@Valid BusGoodsAddNewParam busGoodsAddNewParam) {
        log.debug("开始处理【新增商品】的请求，参数：{}", busGoodsAddNewParam);
        busGoodsService.addNewBusGoods(busGoodsAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/delete")
    @ApiOperation("根据ID删除商品")
    @ApiOperationSupport(order = 202)
    @PreAuthorize("hasAuthority('/busGoods/delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult deleteBusGoodsById(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除商品】的请求，参数：{}", id);
        busGoodsService.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id}/getById")
    @ApiOperation("根据ID查询商品")
    @ApiOperationSupport(order = 252)
   @PreAuthorize("hasAuthority('/busGoods/getById')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult getBusProviderById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询商品】的请求，参数：{}", id);
        busGoodsService.getStandardByGoodsId(id);
        return JsonResult.ok(busGoodsService.getStandardByGoodsId(id));
    }

    @PostMapping("/update")
    @ApiOperation("修改商品信息")
    @ApiOperationSupport(order = 302)
    @PreAuthorize("hasAuthority('/busGoods/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "providerId", value = "商品ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "promitCode", value = "批准文号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "producePlace", value = "商品产地", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "规格", dataType = "String"),
            @ApiImplicitParam(name = "goodsPackage", value = "包装", dataType = "String"),
            @ApiImplicitParam(name = "productCode", value = "生产批号", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "商品描述", dataType = "String"),
            @ApiImplicitParam(name = "price", value = "销售价格", dataType = "String"),
            @ApiImplicitParam(name = "number", value = "数量", dataType = "String"),
            @ApiImplicitParam(name = "dangerNum", value = "库存预警值", dataType = "String"),
            @ApiImplicitParam(name = "goodsImg", value = "商品图片", dataType = "String"),
            @ApiImplicitParam(name = "enable", value = "是否启用", dataType = "Integer")
    })
    public JsonResult updateBusGoodsrById(BusGoods busGoods) {
        log.debug("开始处理【修改商品信息】的请求，参数：{}", busGoods);
        busGoodsService.update(busGoods);
        return JsonResult.ok();
    }

    @PostMapping("/query-get")
    @ApiOperation("模糊查询商品信息")
    @ApiOperationSupport(order = 428)
    @PreAuthorize("hasAuthority('/busGoods/query-get')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value = "商品名称",  dataType = "String"),
            @ApiImplicitParam(name = "providerId", value = "商品ID", dataType = "Long"),
            @ApiImplicitParam(name = "promitCode", value = "批准文号",  dataType = "String"),
            @ApiImplicitParam(name = "producePlace", value = "商品产地", dataType = "String"),
            @ApiImplicitParam(name = "productCode", value = "生产批号", dataType = "String"),
            @ApiImplicitParam(name = "description", value = "商品描述", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult queryByKeyWord(Integer page, @Param("goodsName")String goodsName, @Param("providerId")Long providerId,@Param("promitCode") String promitCode,@Param("producePlace") String producePlace,@Param("productCode") String productCode, @Param("description")String description){
        log.debug("开始处理【模糊查询商品信息】的请求，参数：{},{},{},{},{},{} ", goodsName, providerId,promitCode,producePlace,productCode, description);
        PageData<BusGoodsStandardVO> result  =  busGoodsService.queryByKeyWord(page,goodsName, providerId,promitCode,producePlace,productCode, description);
        System.out.println(result);
        return JsonResult.ok(result);
    }


    @GetMapping("/get-id")
    @ApiOperation("查询商品列表")
    @ApiOperationSupport(order = 429)
    @PreAuthorize("hasAuthority('/busGoods/get-id')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询商品列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusGoods> pageData = busGoodsService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/get-idAsc")
    @ApiOperation("升序查询商品列表")
    @ApiOperationSupport(order = 430)
    @PreAuthorize("hasAuthority('/busGoods/get-idAsc')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult listAsc(Integer page) {
        log.debug("开始处理【升序查询商品列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusGoods> pageData = busGoodsService.listAsc(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/get-idDesc")
    @ApiOperation("降序查询商品列表")
    @ApiOperationSupport(order = 431)
    @PreAuthorize("hasAuthority('/busGoods/get-idDesc')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult listDesc(Integer page) {
        log.debug("开始处理【降序查询商品列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusGoods> pageData = busGoodsService.listDesc(pageNum);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/ban/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/busGoods/ban/update')")
    @ApiOperation("启用商品")
    @ApiOperationSupport(order = 314)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult setGoodsEnable(@PathVariable Long id) {
        log.debug("开始处理【启用商品】的请求，参数：{}", id);
        busGoodsService.setGoodsEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/ban/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/busGoods/noban/update')")
    @ApiOperation("禁用商品")
    @ApiOperationSupport(order = 315)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品ID", required = true, dataType = "long")
    })
    public JsonResult setGoodsDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用商品】的请求，参数：{}", id);
        busGoodsService.setGoodsDisable(id);
        return JsonResult.ok();
    }

}
